package com.zhiyou100.file;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ReUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件切割
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-09 09:44
 */
public class FileToExcel {
    /**
     * 原始文件路径
     */
    public static final String FILE_PATH = "/Users/yang/Downloads/test/files/title.txt";
    /**
     * 所有的标题的路径
     */
    public static final String FILE_PATH_ALL_TITLES = "/Users/yang/Downloads/test/files/all-title.txt";
    /**
     * 所有的主体
     */
    public static final String FILE_PATH_ALL_BODY = "/Users/yang/Downloads/test/files/all-body.txt";
    /**
     * Excel 文件
     */
    public static final String FILE_PATH_ALL = "/Users/yang/Downloads/test/files/title.xlsx";
    /**
     * 占位符
     */
    public static final String PLACEHOLDER = "----------------------";


    public static void main(String[] args) {
        //getAllTitles();
        //replaceAll();


        //// 把整个文件读取出来
        FileReader fileReader = new FileReader(FILE_PATH_ALL_BODY);
        // 读出整个文件
        String result = fileReader.readString();
        final String[] split = result.split(PLACEHOLDER);

        for (int i = 0; i < split.length; i++) {
            if (split[i].length() != 0 || " ".equals(split[i])) {
                System.out.print("i = " + i);
                System.err.println(split[i]);
            }

        }


    }

    public static void writer() {
        // 读取所有的标题
        final List<String> strings = FileUtil.readLines(FILE_PATH_ALL_TITLES, "utf-8");
        //通过工具类创建 writer
        ExcelWriter writer = ExcelUtil.getWriter(FILE_PATH_ALL);

        final ArrayList<String> row1 = CollUtil.newArrayList(strings);

        final ArrayList<String> rows = CollUtil.newArrayList(row1);
        // 一次性写出
        writer.write(rows, true);
        // 关闭
        writer.close();
    }

    public static void replaceAll() {
        // 把整个文件读取出来
        FileReader fileReader = new FileReader(FILE_PATH);
        // 读出整个文件
        String result = fileReader.readString();
        // 记录所有的主体文件
        final FileWriter fileWriter = new FileWriter(FILE_PATH_ALL_BODY);

        // 77、GC是什么? 为什么要有GC?
        // 定义正则，匹配所有
        String regex = "[0-9]+[\\u3001]{1}[\\u4e00-\\u9fa5\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300ba-z_A-Z0-9-\\.!@#\\$%\\\\\\^&\\*\\)\\(\\+=\\{\\}\\[\\]\\/\",'<>~\\·`\\?:;| ？]*";

        String all = ReUtil.replaceAll(result, regex, PLACEHOLDER);

        fileWriter.write(all);
    }


    /**
     * 读取所有的标题
     */
    private static void getAllTitles() {
        // 把整个文件读取出来
        FileReader fileReader = new FileReader(FILE_PATH);
        // 读出整个文件
        String result = fileReader.readString();
        // 记录所有的标题的文件
        final FileWriter fileWriter = new FileWriter(FILE_PATH_ALL_TITLES);

        // 77、GC是什么? 为什么要有GC?
        // 定义正则，匹配所有
        String regex = "[0-9]+[\\u3001]{1}[\\u4e00-\\u9fa5\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300ba-z_A-Z0-9-\\.!@#\\$%\\\\\\^&\\*\\)\\(\\+=\\{\\}\\[\\]\\/\",'<>~\\·`\\?:;| ？]*";

        final ArrayList<String> all = ReUtil.findAll(regex, result, 0, new ArrayList<String>());

        for (String s : all) {
            fileWriter.append(s + "\n");
        }
    }
}
